#! /bin/sh
# 
# Copyright 1999-2006 University of Chicago
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 

DELIM="#"
EXEC="org.globus.bootstrap.Bootstrap br.unb.slaauditor.clients.NegotiateAuditors"
DEF_OPTIONS=""
DEF_CMD_OPTIONS=""
EGD_DEVICE="/dev/urandom"

updateOptions() {
     
  if [ "X$2" != "X" ] ; then
    GLOBUS_OPTIONS="$GLOBUS_OPTIONS -D$1=$2"
  fi

}

####### MAIN BODY ##########

if [ ! -d "$GLOBUS_LOCATION" ] ; then
  echo "Error: GLOBUS_LOCATION invalid or not set: $GLOBUS_LOCATION" 1>&2
  exit 1
fi

# gt4.2: LOCALCLASSPATH=$GLOBUS_LOCATION/lib/common/bootstrap.jar:$GLOBUS_LOCATION/lib/common/cog-url.jar:$GLOBUS_LOCATION/lib/common/axis-url.jar
LOCALCLASSPATH=$GLOBUS_LOCATION/lib/bootstrap.jar:$GLOBUS_LOCATION/lib/cog-url.jar:$GLOBUS_LOCATION/lib/axis-url.jar

### SETUP OTHER VARIABLES ####

updateOptions "GLOBUS_HOSTNAME" "$GLOBUS_HOSTNAME"
updateOptions "GLOBUS_TCP_PORT_RANGE" "$GLOBUS_TCP_PORT_RANGE"
updateOptions "GLOBUS_TCP_SOURCE_PORT_RANGE" "$GLOBUS_TCP_SOURCE_PORT_RANGE"
updateOptions "GLOBUS_UDP_SOURCE_PORT_RANGE" "$GLOBUS_UDP_SOURCE_PORT_RANGE"

if [ -c "$EGD_DEVICE" -a  -r "$EGD_DEVICE" ]; then
    updateOptions "java.security.egd" "file://$EGD_DEVICE"
fi

if [ "X$IBM_JAVA_OPTIONS" = "X" ] ; then
  IBM_JAVA_OPTIONS=-Xquickstart
  export IBM_JAVA_OPTIONS
fi

if [ $# -gt 0 ]; then
  if [ "X${DEF_CMD_OPTIONS}" != "X" ]; then
    set - ${GLOBUS_OPTIONS} -classpath "${LOCALCLASSPATH}" ${EXEC} ${DEF_CMD_OPTIONS} "$@"
  else
    set - ${GLOBUS_OPTIONS} -classpath "${LOCALCLASSPATH}" ${EXEC} "$@"
  fi
else
  if [ "X${DEF_CMD_OPTIONS}" != "X" ]; then
    set - ${GLOBUS_OPTIONS} -classpath "${LOCALCLASSPATH}" ${EXEC} ${DEF_CMD_OPTIONS}
  else
    set - ${GLOBUS_OPTIONS} -classpath "${LOCALCLASSPATH}" ${EXEC}
  fi
fi

OLD_IFS=${IFS}
IFS=${DELIM}
for i in ${DEF_OPTIONS} ; do
  IFS=${OLD_IFS}
  DEFINE=`echo $i|cut -d'=' -f1`
  if [ "$DEFINE" != "$i" ]; then
    VALUE="`echo $i|cut -d'=' -f2-`"
    set - $DEFINE="$VALUE" "$@"
  else
    set - $DEFINE "$@"
  fi
  IFS=${DELIM}
done
IFS=${OLD_IFS}


set - -DGLOBUS_LOCATION="$GLOBUS_LOCATION" -Djava.endorsed.dirs="$GLOBUS_LOCATION/endorsed" "$@"

if [ "X$X509_USER_PROXY" != "X" ] ; then
  set - -DX509_USER_PROXY="$X509_USER_PROXY" "$@"
fi

if [ "X$X509_USER_CERT" != "X" ] ; then
  set - -DX509_USER_CERT="$X509_USER_CERT" "$@"
fi

if [ "X$X509_USER_KEY" != "X" ] ; then
  set - -DX509_USER_KEY="$X509_USER_KEY" "$@"
fi

if [ "X$X509_CERT_DIR" != "X" ] ; then
  set - -DX509_CERT_DIR="$X509_CERT_DIR" "$@"
fi


### EXECUTE ############

if [ "X$JAVA_HOME" = "X" ] ; then
  _RUNJAVA=java
 else 
  _RUNJAVA="$JAVA_HOME"/bin/java
fi

exec $_RUNJAVA "$@"
